package com.aide.simplification.popedom.taglib;

import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.ecs.html.Input;
import org.apache.ecs.html.Label;
import org.apache.ecs.html.Script;

import com.aide.simplification.popedom.popedom.Dept;

public class DeptCheckboxTag extends TagSupport {
	private static final long serialVersionUID = -7532962297785626654L;
	private String name = "";
	private String id = "";
	private String style="";
	private String splitsign=",";
	private int linesize=3;
	private String attributes = "";
	private String defaultValue="";
	
	
	public int doEndTag() throws JspException {
//		ServletRequest request = pageContext.getRequest();
//		ServletResponse response = pageContext.getResponse();
		JspWriter out = pageContext.getOut();
//		ApplicationContext ctx = WebApplicationContextUtils
//				.getWebApplicationContext(pageContext.getServletContext());
//		Dept dept = (Dept) ctx.getBean("Dept");
		Dept dept = new Dept();
		Input input = new Input();
		input.setType("hidden");
		input.setName(name);
		input.setID(name);
		input.setValue("");
		
		
//		if(!attributes.equals("")){
//			Pattern pattern = Pattern.compile("\\s+");
//			String[] attribute = pattern.split(attributes);
//			for (int i = 0; i < attribute.length; i++) {
//				String[] temp = attribute[i].split("=");
//				if(temp!=null&&temp.length==2){
//					select.addAttribute(temp[0], temp[1]);
//				}else if(temp.length==1){
//					select.addAttribute(temp[0], "");
//				}
//			}
//		}
		Script script = new Script();
		script.setType("text/javascript");
		script.setTagText("function checkOn(){$('#"+name+"').val('');$('input[@id="+id+"][@checked]').each(function(){$('#"+name+"').val($('#"+name+"').val()+'"+splitsign+"'+$('#'+$(this).val()).text());});\n var temp= $('#"+name+"').val(); \n if(temp.indexOf('"+splitsign+"')!=-1){temp=temp.substring(1);}\n$('#"+name+"').val(temp);}");
		List<Map<String, Object>> list = dept.findAllDeptIsUse();
		int i=0;
		for (Iterator<Map<String, Object>> iterator = list.iterator(); iterator
				.hasNext();) {
			
			Map<String, Object> map = iterator.next();
			String deptid=map.get("deptid").toString();
			Input checkbox = new Input();
			checkbox.setType("checkbox");
			checkbox.setID(id);
			checkbox.setName(id);
			checkbox.setValue(deptid);
			Label label = new Label();
			label.setID(deptid);
			label.setTagText(map.get("dept").toString());
			if((this.defaultValue.indexOf(deptid))!=-1){
				checkbox.setChecked(true);
			}
			checkbox.setOnClick("checkOn()");
			
			try {
				out.println(checkbox.toString());
				out.println(label.toString());
				if(i%linesize==(linesize-1)){
					out.println("<br />");
				}
			} catch (IOException e) {
			}
			i++;
		}
		
		try {
			out.println(script.toString());
			
			out.println(input.toString());
		} catch (IOException e) {
		}
		return EVAL_PAGE;
	}


	public String getDefaultValue() {
		return defaultValue;
	}

	public void setDefaultValue(String defaultValue) {
		this.defaultValue = defaultValue;
	}

	public String getAttributes() {
		return attributes;
	}

	public void setAttributes(String attributes) {
		this.attributes = attributes.trim();
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getStyle() {
		return style;
	}

	public void setStyle(String style) {
		this.style = style;
	}

	public String getSplitsign() {
		return splitsign;
	}

	public void setSplitsign(String splitsign) {
		this.splitsign = splitsign;
	}

	public int getLinesize() {
		return linesize;
	}

	public void setLinesize(int linesize) {
		if(linesize>0){
			this.linesize = linesize;
		}
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
}
